package org.xtext.tcc.generator;

import com.google.inject.Inject;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.generator.IFileSystemAccess;
import org.eclipse.xtext.generator.IGenerator;
import org.eclipse.xtext.naming.IQualifiedNameProvider;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.xbase.lib.ComparableExtensions;
import org.eclipse.xtext.xbase.lib.IntegerExtensions;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.StringExtensions;
import org.eclipse.xtext.xtend2.lib.ResourceExtensions;
import org.eclipse.xtext.xtend2.lib.StringConcatenation;
import org.xtext.tcc.generator.AspectExtensions;
import org.xtext.tcc.generator.ControllerExtensions;
import org.xtext.tcc.generator.ModelExtensions;
import org.xtext.tcc.tccdsl.Entity;

@SuppressWarnings("all")
public class TccdslGenerator implements IGenerator {
  
  @Inject
  private IQualifiedNameProvider nameProvider;
  
  @Inject
  private ModelExtensions models;
  
  @Inject
  private AspectExtensions aspects;
  
  @Inject
  private ControllerExtensions controllers;
  
  public void doGenerate(final Resource resource, final IFileSystemAccess fsa) {
    {
      Iterable<EObject> _allContentsIterable = ResourceExtensions.allContentsIterable(resource);
      Iterable<Entity> _filter = IterableExtensions.<Entity>filter(_allContentsIterable, org.xtext.tcc.tccdsl.Entity.class);
      for (Entity e : _filter) {
        {
          QualifiedName _fullyQualifiedName = this.nameProvider.getFullyQualifiedName(e);
          List<String> _segments = _fullyQualifiedName.getSegments();
          List<String> fileNames = _segments;
          String entityFileName = "";
          String aspectFileName = "";
          String controllerFileName = "";
          int i = 1;
          for (String fileName : fileNames) {
            {
              int _size = fileNames.size();
              boolean _operator_lessThan = ComparableExtensions.<Integer>operator_lessThan(((Integer)i), ((Integer)_size));
              if (_operator_lessThan) {
                {
                  String _operator_plus = StringExtensions.operator_plus(entityFileName, fileName);
                  String _operator_plus_1 = StringExtensions.operator_plus(_operator_plus, "/");
                  entityFileName = _operator_plus_1;
                  String _operator_plus_2 = StringExtensions.operator_plus(aspectFileName, fileName);
                  String _operator_plus_3 = StringExtensions.operator_plus(_operator_plus_2, "/");
                  aspectFileName = _operator_plus_3;
                  String _operator_plus_4 = StringExtensions.operator_plus(controllerFileName, fileName);
                  String _operator_plus_5 = StringExtensions.operator_plus(_operator_plus_4, "/");
                  controllerFileName = _operator_plus_5;
                }
              } else {
                {
                  String _operator_plus_6 = StringExtensions.operator_plus(entityFileName, fileName);
                  String _operator_plus_7 = StringExtensions.operator_plus(_operator_plus_6, ".java");
                  entityFileName = _operator_plus_7;
                  String _operator_plus_8 = StringExtensions.operator_plus(aspectFileName, "aspect/Aspecto");
                  String _firstUpper = StringExtensions.toFirstUpper(fileName);
                  String _operator_plus_9 = StringExtensions.operator_plus(_operator_plus_8, _firstUpper);
                  String _operator_plus_10 = StringExtensions.operator_plus(_operator_plus_9, ".aj");
                  aspectFileName = _operator_plus_10;
                  String _operator_plus_11 = StringExtensions.operator_plus(controllerFileName, "control/");
                  String _firstUpper_1 = StringExtensions.toFirstUpper(fileName);
                  String _operator_plus_12 = StringExtensions.operator_plus(_operator_plus_11, _firstUpper_1);
                  String _operator_plus_13 = StringExtensions.operator_plus(_operator_plus_12, "Controller.java");
                  controllerFileName = _operator_plus_13;
                }
              }
              int _operator_plus_14 = IntegerExtensions.operator_plus(((Integer)i), ((Integer)1));
              i = _operator_plus_14;
            }
          }
          StringConcatenation _compile = this.models.compile(e);
          fsa.generateFile(entityFileName, _compile);
          StringConcatenation _aspect = this.aspects.aspect(e);
          fsa.generateFile(aspectFileName, _aspect);
          StringConcatenation _control = this.controllers.control(e);
          fsa.generateFile(controllerFileName, _control);
        }
      }
      boolean generateMain = true;
      if (generateMain) {
        StringConcatenation _app = this.app(((Boolean)generateMain));
        fsa.generateFile("app/init/AppInit.java", _app);
      }
    }
  }
  
  public StringConcatenation app(final Object o) {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package app.init;");
    _builder.newLine();
    _builder.newLine();
    _builder.append("public class AppInit{");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("public static void main(String args[]){");
    _builder.newLine();
    _builder.append("\t\t");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("}");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    return _builder;
  }
}